package com.tanhua.server.controller;

import cn.hutool.core.util.ObjectUtil;
import com.tanhua.common.vo.PageResult;
import com.tanhua.dubbo.vo.CountsVo;
import com.tanhua.dubbo.vo.SettingsVo;
import com.tanhua.server.service.MyCenterService;
import com.tanhua.server.vo.UserInfoVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import java.util.Map;

/**
 * @author Don
 */
@RequestMapping("users")
@RestController
@Slf4j
public class MyCenterController {

    @Autowired
    private MyCenterService myCenterService;

    /**
     * 根据用户id查询用户信息
     *
     * @param userId 用户id，如果为空，表示查询当前登录人的信息
     * @return
     */
    @GetMapping
    public ResponseEntity<UserInfoVo> queryUserInfoByUserId(@RequestParam(value = "userID", required = false) Long userId) {
        try {
            UserInfoVo userInfoVo = this.myCenterService.queryUserInfoByUserId(userId);
            if (ObjectUtil.isNotEmpty(userInfoVo)) {
                return ResponseEntity.ok(userInfoVo);
            }
        } catch (Exception e) {
            log.error("根据用户id查询用户信息出错~ userId = " + userId, e);
        }
        return ResponseEntity.status(500).build();
    }


    /**
     * 修改用户资料
     *
     * @param userInfoVo
     * @return
     */
    @PutMapping
    public ResponseEntity<Void> updateUserInfoByUser(@RequestBody UserInfoVo userInfoVo){
        try {
            Boolean flag = this.myCenterService.updateUserInfoByUser(userInfoVo);
            if(flag){
                return ResponseEntity.ok(null);
            }
        } catch (Exception e) {
            log.error("修改资料失败 ~ " ,e);
        }
        return ResponseEntity.status(500).build();
    }
	
	
    @GetMapping("{uid}/alreadyLove")
    public ResponseEntity<Object> isAlreadyLove(@PathVariable("uid")Long likeUserId){
       try {
		   Boolean flag= this.myCenterService.isAlreadyLove(likeUserId);
		   if(flag){
				return ResponseEntity.ok(flag);
			}
		}catch (Exception e) {
            log.error("喜欢失败 ~ " ,e);
        }
		return ResponseEntity.status(500).build();
    }

    /**
     * 互相喜欢，喜欢，粉丝 - 统计
     *
     * @return
     */
    @GetMapping("counts")
    public ResponseEntity<CountsVo> queryCounts(){
        try {
            CountsVo countsVo = this.myCenterService.queryCounts();
            if(ObjectUtil.isNotEmpty(countsVo)){
                return ResponseEntity.ok(countsVo);
            }
        } catch (Exception e) {
            log.error("统计相互喜欢数、喜欢数以及粉丝数有误 ~ ",e);
        }
        return ResponseEntity.status(500).build();
    }


    /**
     * 互相喜欢、喜欢、粉丝、谁看过我 - 翻页列表
     *
     * @param type
     * @param page
     * @param pageSize
     * @param nickName
     * @return
     */
    @GetMapping("friends/{type}")
    public ResponseEntity<PageResult> queryLikeList(@PathVariable("type") String type,
                                                    @RequestParam(value = "page",defaultValue = "1") Integer page,
                                                    @RequestParam(value = "pagesize" , defaultValue = "10") Integer pageSize,
                                                    @RequestParam(value = "nickname" , required = false) String nickName){

        try {
            page = Math.max(1,page);
            PageResult pageResult = this.myCenterService.queryLikeList(Integer.valueOf(type), page, pageSize, nickName);
            if(ObjectUtil.isNotEmpty(pageResult)){
                return ResponseEntity.ok(pageResult);
            }
        } catch (Exception e) {
            log.error("翻页列表出现错误 ~ ",e);
        }
        return ResponseEntity.status(500).build();
    }


    /**
     * 取消喜欢
     *
     * @return
     */
    @DeleteMapping("like/{uid}")
    public ResponseEntity<Void> disLike(@PathVariable("uid") Long userId){
        try {
            Boolean flag = this.myCenterService.disLike(userId);
            if(flag){
                return ResponseEntity.ok(null);
            }
        } catch (Exception e) {
            log.error("取消好友关系失败 ~ userId = " + userId,e);
        }
        return ResponseEntity.status(500).build();
    }

    /**
     * 粉丝 - 喜欢
     *
     * @param userId
     * @return
     */
    @PostMapping("fans/{uid}")
    public ResponseEntity<Void> LoveFan(@PathVariable("uid") Long userId){
        try {
            Boolean flag = this.myCenterService.LoveFan(userId);
            if(flag){
                return ResponseEntity.ok(null);
            }
        } catch (Exception e) {
            log.error("对粉丝喜欢失败 ~ userId = " + userId,e);
        }
        return ResponseEntity.status(500).build();
    }


    /**
     *  用户通用设置 - 读取
     *
     * @return
     */
    @GetMapping("settings")
    public ResponseEntity<SettingsVo> queryNotification(){
        try {
            SettingsVo settingsVo = this.myCenterService.queryNotification();
            if(ObjectUtil.isNotEmpty(settingsVo)){
                return ResponseEntity.ok(settingsVo);
            }
        } catch (Exception e) {
            log.error("读取用户通用设置出现错误~",e);
        }
        return ResponseEntity.status(500).build();
    }

    /**
     * 设置陌生人问题 - 保存
     * @param map
     * @return
     */
    @PostMapping("questions")
    public ResponseEntity<Void> saveQuestions(@RequestBody Map<String,String> map){
        try {
            // 获取问题
            String content = map.get("content");
            this.myCenterService.saveQuestions(content);
            return ResponseEntity.ok(null);
        } catch (Exception e) {
            log.error("保存陌生人问题出现错误~",e);
        }
        return ResponseEntity.status(500).build();
    }


    /**
     * 通知设置 - 保存
     *
     * @param map
     * @return
     */
    @PostMapping("notifications/setting")
    public ResponseEntity<Void> updateNotification(@RequestBody Map<String,String> map){
        try {
            // 获取喜欢、评论、公告通知
            Boolean likeNotification = Boolean.valueOf(map.get("likeNotification"));
            Boolean pinglunNotification = Boolean.valueOf(map.get("pinglunNotification"));
            Boolean gonggaoNotification = Boolean.valueOf(map.get("gonggaoNotification"));

            this.myCenterService.updateNotification(likeNotification,pinglunNotification,gonggaoNotification);
            return ResponseEntity.ok(null);
        } catch (Exception e) {
            log.error("通知更新失败~",e);
        }
        return ResponseEntity.status(500).build();
    }


    /**
     * 黑名单 - 翻页列表
     *
     * @param page
     * @param pageSize
     * @return
     */
    @GetMapping("blacklist")
    public ResponseEntity<PageResult> queryBlackList( @RequestParam(value = "page",defaultValue = "1") Integer page,
                                     @RequestParam(value = "pagesize" , defaultValue = "10") Integer pageSize){
        try {
            PageResult pageResult = this.myCenterService.queryBlackList(page, pageSize);
            return ResponseEntity.ok(pageResult);
        } catch (Exception e) {
            log.error("黑名单列表查询失败~",e);
        }
        return ResponseEntity.status(500).build();
    }


    /**
     * 黑名单 - 移除
     *
     * @param userId
     * @return
     */
    @DeleteMapping("blacklist/{uid}")
    public ResponseEntity<Void> deleteBlack(@PathVariable("uid") Long userId){
        try {
            this.myCenterService.deleteBlack(userId);
            return ResponseEntity.ok(null);
        } catch (Exception e) {
            log.error("黑名单删除失败~",e);
        }
        return ResponseEntity.status(500).build();
    }
}





